package com.hjqjl.whdemo.net.api;

import com.apep.netlibrary.use.RetrofitFactory;
import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.hjqjl.whdemo.feature.feature0.model.GetImagesModel;
import com.hjqjl.whdemo.feature.feature0.model.SentencesModel;
import com.hjqjl.whdemo.net.HttpManager;
import com.hjqjl.whdemo.net.entity.GetImagesEntity;

import java.util.HashMap;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * wh on 2017/9/28.
 */

public class HttpService {
    private final ApiService apiService;

    private HttpService() {
        apiService = RetrofitFactory.getInstance().create(ApiService.class);
    }

    public static HttpService getInstance() {
        return HttpMethodsHolder.S_INSTANCE;
    }

    private static class HttpMethodsHolder {
        private static final HttpService S_INSTANCE = new HttpService();
    }

    public Observable<GetImagesModel> getImages(String size) {
        HashMap<String, String> map = new HashMap<>();
        map.put("page", "0");
        map.put("size", size);
        return apiService.getImages(map);
    }

    public Observable<GetImagesEntity> getImages2(String size) {
        HashMap<String, String> map = new HashMap<>();
        map.put("page", "0");
        map.put("size", size);
        return apiService.getImages2(map);
    }

    public Observable<String> getImagesString(String size) {
        HashMap<String, String> map = new HashMap<>();
        map.put("page", "1");
        map.put("size", size);
        return apiService.getImagesString(map);
    }

    /**
     *
     */
    public Observable<SentencesModel> getSentences() {
        return apiService.getSentences();
    }

    /**
     * 将请求的json字符串转化为RequestBody
     */
    private RequestBody getBody(HashMap<String, String> params) {
        try {
            String jsonStr = new Gson().toJson(params);
            MediaType mediaType = MediaType.parse("application/json");
            return RequestBody.create(mediaType, jsonStr);
        } catch (Exception e) {
            LogUtils.e("RequestBody is null");
            e.printStackTrace();
        }
        return null;
    }
}
